﻿using System;
using System.Windows.Forms;
using CRMS.Models;
using System.Linq;

namespace CRMS.Views.TeacherMgnt
{
    public partial class frmTeacherInfo : Form
    {
        //Field datas
        private string eventPassParameter = String.Empty;
        private string departmentId = string.Empty;
        private TeacherRep teacherRep = new TeacherRep();
        private DepartmentRep departRep = new DepartmentRep();
        private CourseRep courseRep = new CourseRep();
        private static int pageSize = 6;
        private static int pageCount = 0;
        private static int pageIndex = 0;

        public frmTeacherInfo(string courseID,string departmentId)
        {
            InitializeComponent();
            eventPassParameter = courseID;
            this.position = 0;
            this.departmentId = departmentId;
        }
       
        private void frmTeacherInfo_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
          
            cboxDepartmentName.DataSource = dpr.GetDepartments();
            cboxDepartmentName.DisplayMember = "DepartmentName";
            cboxDepartmentName.ValueMember = "DepartmentId";
           
            if (pageIndex == 0)
                FirstPage.Enabled = false;
           
            if (eventPassParameter == String.Empty && this.departmentId == String.Empty)
            {
                pageCount = teacherRep.GetTeachers().Count() / pageSize;
                dataGridView1.DataSource = teacherRep.GetTeachers().Skip(pageIndex * pageSize).Take(pageSize);
            }
            else if (eventPassParameter == String.Empty)
            {
                pageCount = teacherRep.GetTeacherByDID(this.departmentId).Count() / pageSize;
                dataGridView1.DataSource = teacherRep.GetTeacherByDID(this.departmentId);
            }
            else
            {
                pageCount = teacherRep.GetTeachersByCourseID(eventPassParameter).Count() / pageSize;
                dataGridView1.DataSource = teacherRep.GetTeachersByCourseID(eventPassParameter).Skip(pageIndex * pageSize).Take(pageSize);
            }

            if (pageIndex == pageCount)
                NextPage.Enabled = false;
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            SetDefaultValue();
            txtTeacherId.Text = "";
            txtTeacherName.Text = "";
            txtTeacherSex.Text = "";
            txtTeacherTitle.Text = "";
            txtIndicate.Text = "";
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            setDefualtStatus();
            if (dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value == null)
            {
                return;
            }
            else
            {
                txtTeacherId.Text = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                txtTeacherName.Text = dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                txtTeacherSex.Text = dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                cbDepartID.Text = dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                txtTeacherTitle.Text = dataGridView1[4, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                txtIndicate.Text = dataGridView1[5, dataGridView1.CurrentCell.RowIndex].Value.ToString();
            }
        }

        private void TstripBtnModify_Click(object sender, EventArgs e)
        {
            SetDefaultValue();
            txtTeacherId.ReadOnly = true;
            TstripBtnSave.Click -= new EventHandler(this.TstripBtnSave_Click);
            TstripBtnSave.Click += new EventHandler(this.TstripBtnSave_Change);
        }

        private void TstripBtnSave_Change(object sender, EventArgs e)
        {
            setDefualtStatus();
            Teacher teacher = new Teacher();
            teacher = teacherRep.GetTeacherByID(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString());
            if (teacher == null)
            {
                MessageBox.Show("该老师不存在或者已经被删除！");
            }
            else
            {
                teacher.TeacherId = txtTeacherId.Text.Trim();
                teacher.TeacherName = txtTeacherName.Text.Trim();
                teacher.TeacherSex = txtTeacherSex.Text.Trim();
                teacher.TeacherTitle = txtTeacherTitle.Text.Trim();
                teacher.TeacherIndicate = txtIndicate.Text.Trim();
                teacher.DepartmentId = ((Department)cbDepartID.Items[cbDepartID.SelectedIndex]).DepartmentId;
                DialogResult result = MessageBox.Show("确定修改？", "修改提示", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    teacherRep.Save();
                    TstripBtnSave.Click -= new EventHandler(this.TstripBtnSave_Change);
                    frmTeacherInfo_Load(sender, e);
                }
                else
                {
                    return;
                }
            }

        }

        private void TstripBtnSave_Click(object sender, EventArgs e)
        {
            setDefualtStatus();
            Teacher teacher = new Teacher();
            if(txtTeacherId.Text.Trim() == String.Empty
               ||txtTeacherName.Text.Trim() == String.Empty 
               ||txtTeacherSex.Text.Trim() == String.Empty
               ||txtTeacherTitle.Text.Trim() == String.Empty
               ||txtIndicate.Text.Trim() == String.Empty)
            {
                MessageBox.Show("所有字段不能为空！");
            }
            if (teacherRep.GetTeacherByID(txtTeacherId.Text.Trim()) != null)
            {
                MessageBox.Show("该老师已经存在，不能重复添加！");
            }
            else
            {
                 teacher.TeacherId = txtTeacherId.Text.Trim();
                 teacher.TeacherName = txtTeacherName.Text.Trim();
                 teacher.TeacherSex = txtTeacherSex.Text.Trim();
                 teacher.TeacherTitle = txtTeacherTitle.Text.Trim();
                 teacher.TeacherIndicate = txtIndicate.Text.Trim();
                 teacher.DepartmentId = ((Department)cbDepartID.Items[cbDepartID.SelectedIndex]).DepartmentId;
                  DialogResult result = MessageBox.Show("是否确定添加？","添加提示",MessageBoxButtons.OKCancel);
                  if (result == DialogResult.OK)
                  {
                      teacherRep.Add(teacher);
                      teacherRep.Save();
                      frmTeacherInfo_Load(sender, e);
                  }
                  else
                  {
                      return;
                  }
            }
        }

        private void setDefualtStatus()
        {
            txtIndicate.ReadOnly = true;
            txtTeacherId.ReadOnly = true;
            txtTeacherName.ReadOnly = true;
            txtTeacherSex.ReadOnly = true;
            txtTeacherTitle.ReadOnly = true;
            cbDepartID.Enabled = false;
            
        }

        private void SetDefaultValue()
        {
            txtIndicate.ReadOnly = false;
            txtTeacherId.ReadOnly = false;
            txtTeacherName.ReadOnly = false;
            txtTeacherSex.ReadOnly = false;
            txtTeacherTitle.ReadOnly = false;
            cbDepartID.Enabled = true;
            cbDepartID.DataSource = departRep.GetDepartments();
            cbDepartID.DisplayMember = "DepartmentName";
            cbDepartID.ValueMember = "DepartmentId";
        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            Teacher teacher = new Teacher();
            if (dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value == null)
            {
                MessageBox.Show("请先选择您要删除的老师！");
            }
            else
            {
                teacher = teacherRep.GetTeacherByID(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString());
                if (teacher == null)
                {
                    MessageBox.Show("该老师已经被删除或者不存在！");
                }
                else
                {
                    DialogResult result = MessageBox.Show("您确定删除该老师？","删除提示",MessageBoxButtons.OKCancel);
                    if (result == DialogResult.OK)
                    {
                        teacherRep.Delete(teacher);
                        teacherRep.Save();
                        frmTeacherInfo_Load(sender, e);
                    }
                }
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOpenCourseInfo_Click(object sender, EventArgs e)
        {
            if (frmMain.fm.CheckForm("frmOpenCourseInfo") == true)
            {
                return;
            }
            else
            {
                frmOpenCourseInfo tocInfo = new frmOpenCourseInfo(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString(),
                    dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString());
                tocInfo.MdiParent = frmMain.fm;
                this.Close();
                tocInfo.Show();
            }
        }
        //首记录
        private void tsbtnFirstRecord_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.RowCount != 0)
            {
                if (this.position != 0)
                {
                    foreach (DataGridViewRow dgvr in this.dataGridView1.Rows)
                    {
                        dgvr.Selected = false;
                    }
                    this.dataGridView1.Rows[0].Selected = true;
                    this.position = 0;
                }
            }

        }
        //上一条记录
        private void tsbtnUpRecord_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.RowCount != 0)
            {
                if (this.position > 0)
                {
                    foreach (DataGridViewRow dgvr in this.dataGridView1.Rows)
                    {
                        dgvr.Selected = false;
                    }
                    this.position--;
                    this.dataGridView1.Rows[this.position].Selected = true;
                }
            }
        }
        //下一条记录
        private void tsbtnDownRecord_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.RowCount != 0)
            {
                if (this.position < this.dataGridView1.Rows.Count - 1)
                {
                    foreach (DataGridViewRow dgvr in this.dataGridView1.Rows)
                    {
                        dgvr.Selected = false;
                    }
                    this.position++;
                    this.dataGridView1.Rows[this.position].Selected = true;
                }
            }
        }
        //尾记录
        private void tsbtnLastRecord_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.RowCount != 0)
            {
                if (this.position != this.dataGridView1.Rows.Count - 1)
                {
                    foreach(DataGridViewRow dgvr in this.dataGridView1.Rows)
                    {
                        dgvr.Selected = false;
                    }
                    this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 1].Selected = true;
                    this.position = this.dataGridView1.Rows.Count - 1;
                }
            }

        }

        private void tsbtnAdd_Click(object sender, EventArgs e)
        {
            bindingNavigatorAddNewItem_Click(sender, e);
        }

        private void btnRetrive_Click(object sender, EventArgs e)
        {
            string departmentId = ((Department)cboxDepartmentName.Items[cboxDepartmentName.SelectedIndex]).DepartmentId;
            string courseName = txtCourseName.Text.Trim();

            dataGridView1.DataSource =
                departmentId != String.Empty && courseName == String.Empty ?teacherRep.GetTeacherByDID(departmentId) : 
                departmentId == String.Empty && courseName != String.Empty ?courseRep.GetTeachersByCourseName(courseName) : 
                departmentId != String.Empty && courseName != String.Empty ?teacherRep.getTeachersInfo(departmentId,txtCourseName.Text.Trim()) : 
                teacherRep.GetTeachers();
        }

        private void NextPage_Click(object sender, EventArgs e)
        {
            if (pageIndex < pageCount)
            {
                pageIndex++;
                FirstPage.Enabled = true;
                frmTeacherInfo_Load(sender, e);
            }
            else
            {
                NextPage.Enabled = false; ;
            }
        }

        private void FirstPage_Click(object sender, EventArgs e)
        {
            if (pageIndex > 0)
            {
                pageIndex--;
                NextPage.Enabled = true;
                frmTeacherInfo_Load(sender, e);
            }
            else
            {
                FirstPage.Enabled = false;
            }
        }
    }
}
